package filters;

import java.io.IOException;
import java.sql.Timestamp;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Servlet Filter implementation class RegisterValidationFilter
 */
public class RegisterValidationFilter implements Filter {

    /**
     * Default constructor. 
     */
    public RegisterValidationFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here
		
		
		//only do with post request
		HttpServletRequest req = (HttpServletRequest)request;
		
		if (req.getMethod().equals("POST"))
		{
			String errorMsg = "";
			boolean isError = false;
			
			String address1 = request.getParameter("address1");
			String address2 = request.getParameter("address2");
			String city = request.getParameter("city");
			String year = request.getParameter("dobYear");
			String month = request.getParameter("dobMonth");
			String day = request.getParameter("dobDay");
			String email = request.getParameter("email");
			String firstName = request.getParameter("firstName");
			String lastName = request.getParameter("lastName");
			String gender = request.getParameter("gender");
			String password = request.getParameter("password");
			String phone = request.getParameter("phone");
			String state = request.getParameter("state");
			String username = request.getParameter("userName");
			String zip = request.getParameter("zip");
			
			if (address1 == null)
			{
				isError = true;
				errorMsg += "Address 1 is required\n";
			}
			
			if (city == null)
			{
				isError = true;
				errorMsg += "City is required\n";
			}
			
			if (year == null)
			{
				isError = true;
				errorMsg += "Year is required\n";
			}
			
			if (month == null)
			{
				isError = true;
				errorMsg += "Month is required\n";
			}
			
			if (day == null)
			{
				isError = true;
				errorMsg += "Day is required\n";
			}
			
			int iDay = 0;
			int iYear = 0;
			int iMonth = 0;
			
			try
			{
				iYear = Integer.parseInt(year);
				iMonth = Integer.parseInt(month);
				iDay = Integer.parseInt(day);
			}
			catch (NumberFormatException ex)
			{
				isError = true;
				errorMsg += "You have to enter number into the date fields\n";
			}
			
			if (iYear < 1)
			{
				isError = true;
				errorMsg += "Year has to be greater than 0\n";
			}
			
			if (iMonth < 1 || iMonth > 12)
			{
				isError = true;
				errorMsg += "Month has to be between 1 and 12\n";
			}
			
			switch (iMonth)
			{
			case 2:
				if (iYear % 400 == 0 || (iYear % 4 == 0 && iYear % 100 != 0)) //check leap year
				{
					if (iDay < 1 || iDay > 29)
					{
						isError = true;
						errorMsg += "Invalid day\n";
					}
				}
				break;
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				if (iDay < 1 || iDay > 31)
				{
					isError = true;
					errorMsg += "Invalid day\n";
				}
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				if (iDay < 1 || iDay > 30)
				{
					isError = true;
					errorMsg += "Invalid day\n";
				}
				break;
			}
			
			if (email == null)
			{
				isError = true;
				errorMsg += "Email is required\n";
			}
			
			//check email more later
			
			if (firstName == null)
			{
				isError = true;
				errorMsg += "First Name is required\n";
			}
			
			if (lastName == null)
			{
				isError = true;
				errorMsg += "Last Name is required\n";
			}
			
			if (password == null)
			{
				isError = true;
				errorMsg += "Password is required\n";
			}
			
			if (zip == null)
			{
				isError = true;
				errorMsg += "Zipcode is required\n";
			}
			
			if (zip.length() != 5)
			{
				isError = true;
				errorMsg += "Invalid Zipcode\n";
			}
			
			if (isError) //there's error
			{
				HttpSession session = req.getSession();
				
				//set the error message to the return status
				session.setAttribute("ReturnStatus", errorMsg);
				
				//redirect to the register page
				((HttpServletResponse)response).sendRedirect("Register");
			}
			else //no error
			{
				// pass the request along the filter chain
				chain.doFilter(request, response);
			}
		}
		else
		{
			chain.doFilter(request, response);
		}

		
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
